/**
 *
 */
package jp.seraph.same.controller;

import jp.seraph.jsade.action.ActionCreator;
import jp.seraph.jsade.action.AgentAction;
import jp.seraph.jsade.core.World;
import jp.seraph.jsade.math.Angle;
import jp.seraph.jsade.task.AgentTask;
import jp.seraph.jsade.task.TaskSelector;
import jp.seraph.jsmf.motion.Motion;
import jp.seraph.jspf.pose.PoseSet;
import jp.seraph.jspf.utils.PoseUtil;
import jp.seraph.same.model.MotionModel;
import jp.seraph.same.model.PoseModel;

/**
 *
 *
 */
public class DefaultAgentController implements AgentController, TaskSelector {
    public DefaultAgentController(ActionCreator aActionCreator) {
        mCreator = aActionCreator;
    }

    private AgentTask mCurrentTask = null;

    private AgentAction mAction = null;

    private AgentAction mPermanentAction = null;

    private ActionCreator mCreator;

    /**
     *
     * @see jp.seraph.same.controller.AgentController#apply(jp.seraph.jsmf.motion.Motion)
     */
    public void apply(Motion aMotion) {
        mCurrentTask = aMotion.toTask();
    }

    /**
     *
     * @see jp.seraph.same.controller.AgentController#apply(jp.seraph.jspf.pose.Pose)
     */
    public void apply(PoseSet aPose) {
        mCurrentTask = aPose.toTask();
    }

    public void apply(MotionModel aMotion) {
        this.apply(aMotion.toMotion());
    }

    public void apply(PoseModel aPose) {
        this.apply(PoseUtil.createPoseSet(aPose.toPose()));
    }

    /**
     *
     * @see jp.seraph.same.controller.AgentController#beam(double, double, jp.seraph.jsade.math.Angle)
     */
    public void beam(double aX, double aY, Angle aAngle) {
        mAction = mCreator.createBeamAction(aX, aY, aAngle.getDegree());
    }

    /**
     *
     * @see jp.seraph.same.controller.AgentController#endPermanentBeam()
     */
    public void endPermanentBeam() {
        mPermanentAction = null;
    }

    /**
     *
     * @see jp.seraph.same.controller.AgentController#say(java.lang.String)
     */
    public void say(String aMessage) {
        mAction = mCreator.createSayAction(aMessage);
    }

    /**
     *
     * @see jp.seraph.same.controller.AgentController#startPermanentBeam(double, double, jp.seraph.jsade.math.Angle)
     */
    public void startPermanentBeam(double aX, double aY, Angle aAngle) {
        mPermanentAction = mCreator.createBeamAction(aX, aY, aAngle.getDegree());
    }

    /**
     *
     * @see jp.seraph.jsade.task.TaskSelector#getAgentAction(jp.seraph.jsade.core.World)
     */
    public AgentAction getAgentAction(World aWorld) {
        if (mAction != null) {
            AgentAction tAction = mAction;
            mAction = null;
            return tAction;
        }

        return mPermanentAction;
    }

    /**
     *
     * @see jp.seraph.jsade.task.TaskSelector#selectTask(jp.seraph.jsade.core.World)
     */
    public AgentTask selectTask(World aWorld) {
        if (mCurrentTask != null && mCurrentTask.canExecute()) {
            AgentTask tTask = mCurrentTask;
            mCurrentTask = null;
            return tTask;
        }

        return null;
    }
}
